package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportDepartmentRpcService;
import com.hunttown.mes.rpc.domain.TransportDepartmentDTO;
import com.hunttown.mes.rpc.domain.query.TransportDepartmentDTOQuery;
import com.hunttown.mes.service.TransportDepartmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportDepartmentConverts.convert;

/**
 * created by wangjunfu on 2019-04-28 16:29:01
 */
@Service
public class TransportDepartmentRpcServiceImpl implements TransportDepartmentRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportDepartmentRpcServiceImpl.class);

    private final TransportDepartmentService transportDepartmentService;

    @Autowired
    public TransportDepartmentRpcServiceImpl(TransportDepartmentService transportDepartmentService) {
        this.transportDepartmentService = transportDepartmentService;
    }

    @Override
    public TransportDepartmentDTO insert(TransportDepartmentDTO transportDepartmentDTO) {
        try {
            return convert(transportDepartmentService.insert(convert(transportDepartmentDTO)));
        } catch (Exception e) {
            logger.error("TransportDepartment create failed.", e);
            throw new RuntimeException("TransportDepartment create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportDepartmentDTO transportDepartmentDTO) {
        try {
            return transportDepartmentService.updateInfoById(convert(transportDepartmentDTO));
        } catch (Exception e) {
            logger.error("TransportDepartment update by id failed.", e);
            throw new RuntimeException("TransportDepartment update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportDepartmentDTOQuery transportDepartmentDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportDepartmentDTOQuery.getW_id() != null && transportDepartmentDTOQuery.getW_id() > 0) {
                query.put("w_id", transportDepartmentDTOQuery.getW_id());
            }

            if (transportDepartmentDTOQuery.getIds() != null && !transportDepartmentDTOQuery.getIds().equals("")) {
                query.put("ids", transportDepartmentDTOQuery.getIds());
            }

            return transportDepartmentService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportDepartment update by query failed.", e);
            throw new RuntimeException("TransportDepartment update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportDepartmentDTO selectById(Integer id) {
        try {
            return convert(transportDepartmentService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportDepartment select by id failed.", e);
            throw new RuntimeException("TransportDepartment select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportDepartmentDTO selectObjByQuery(TransportDepartmentDTOQuery transportDepartmentDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (StringUtils.isNotBlank(transportDepartmentDTOQuery.getDepName())) {
                query.put("depName", transportDepartmentDTOQuery.getDepName());
            }

            if (StringUtils.isNotBlank(transportDepartmentDTOQuery.getDepNameCn())) {
                query.put("depNameCn", transportDepartmentDTOQuery.getDepNameCn());
            }

            if (transportDepartmentDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportDepartmentDTOQuery.getStopFlag());
            }

            if (transportDepartmentDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportDepartmentDTOQuery.getDeleteFlag());
            }

            return convert(transportDepartmentService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportDepartment select obj by query failed.", e);
            throw new RuntimeException("TransportDepartment select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportDepartmentDTO> getListForPage(TransportDepartmentDTOQuery transportDepartmentDTOQuery) {
        Query query = Query.page(transportDepartmentDTOQuery.getPn(), transportDepartmentDTOQuery.getPageSize());

        if (transportDepartmentDTOQuery.getId() != null) {
            query.put("id", transportDepartmentDTOQuery.getId());
        }

        if (transportDepartmentDTOQuery.getParentId() != null) {
            query.put("parentId", transportDepartmentDTOQuery.getParentId());
        }

        if (transportDepartmentDTOQuery.getDepNameCn() != null && !transportDepartmentDTOQuery.getDepNameCn().equals("")) {
            query.put("depNameCn", transportDepartmentDTOQuery.getDepNameCn());
        }

        if (transportDepartmentDTOQuery.getDepName() != null && !transportDepartmentDTOQuery.getDepName().equals("")) {
            query.put("depName", transportDepartmentDTOQuery.getDepName());
        }

        if (transportDepartmentDTOQuery.getIds() != null && !transportDepartmentDTOQuery.getIds().equals("")) {
            query.put("ids", transportDepartmentDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportDepartmentDTOQuery.getLikeName())) {
            query.put("likeName", transportDepartmentDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(transportDepartmentDTOQuery.getShowDate())) {
            query.put("showDate", transportDepartmentDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportDepartmentDTOQuery.getStartDate())) {
            query.put("startDate", transportDepartmentDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportDepartmentDTOQuery.getEndDate())) {
            query.put("endDate", transportDepartmentDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportDepartmentDTOQuery.getOrderBy())) {
            query.put("orderBy", transportDepartmentDTOQuery.getOrderBy());
        }

        if (transportDepartmentDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportDepartmentDTOQuery.getStopFlag());
        }

        if (transportDepartmentDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportDepartmentDTOQuery.getDeleteFlag());
        }

        List<TransportDepartmentDTO> list = convert(transportDepartmentService.selectByQuery(query));
        Page<TransportDepartmentDTO> page = Page.from(transportDepartmentDTOQuery.getPn(), transportDepartmentDTOQuery.getPageSize(), list);
        page.setTotalItems(transportDepartmentService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportDepartmentService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportDepartment delete failed.", e);
            throw new RuntimeException("TransportDepartment delete failed, Please order your code.", e);
        }
    }
}